<?php

/**	
 *	social add
 *	module that allows user to add content inline
 *
 *	@author			pieter van peteghem <pieter@onedotonly.be>
 *
 *	@copyright		oneDot only <we@onedotonly.be>
 * 	@copyright		Copyright (c) 2010, oneDot only. All rights reserved.
 * 	@license		BSD License
 */


class SocialAdd extends MOD {


	/**
	 *	constructor
	 *	
	 *	add module vars and reference (multiple modules on 1 page are possible)	
	 *	@param		array ($vars)
	 */
	 
	function __construct( $vars ) {
		$this->_addvars( $vars );
		$this->_ref();
	}


	/**
	 * 	define output
	 *	this is the base for rendering the stars at the frontend
	 */
	 
	public function output() {
		
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();

		$this->addclass( $this->action );
		
		if( $result && is_array( $result ))
			$result = '<ul>' . implode( "\n", $result ) . '</ul>';
		
		if( $result )		
			return $this->parse( $result ) . $this->render_js();
		
	}	
	
	
	/**
	 * 	simple action
	 */
	
	public function filter_simple() {
		
		//if( !$this->vars->personal ) {
		
			$Modcon = new MOD_CONSTRUCT();
			$Module = $Modcon->call( $this->vars->id );
		
			unset( $Module->nameid );
		
			$mod = $Module->output();
			$css = file_get_contents( engine_get( 'url' ) . '/style.mod?mods=' . $this->vars->id );
			$jsc = file_get_contents( engine_get( 'url' ) . '/script.mod?mods=' . $this->vars->id );
		
			$res .= '<style type="text/css">' . $css . '</style>';
			$res .= '<script type="text/javascript">' . $jsc . '</script>';
			$res .= '<span class="add">' . translate( $this->vars->txt ) . '</span>';
			$res .= $mod;
		
		/*} else if( $this->vars->personal && !empty( $_SESSION[ 'uid' ] )) { 
		
			$Modcon = new MOD_CONSTRUCT();
			$Module = $Modcon->call( $this->vars->id );
		
			unset( $Module->nameid );
		
			$mod = $Module->output();
			$css = file_get_contents( engine_get( 'url' ) . '/style.mod?mods=' . $this->vars->id );
			$jsc = file_get_contents( engine_get( 'url' ) . '/script.mod?mods=' . $this->vars->id );
		
			$res .= '<style type="text/css">' . $css . '</style>';
			$res .= '<script type="text/javascript">' . $jsc . '</script>';
			$res .= '<span class="add">' . translate( $this->vars->txt ) . '</span>';
			$res .= $mod;
		
		} else {
			
			$res .= '<span class="add">' . translate( $this->vars->txt ) . '</span>';
			$res .= '<div class="mod TextBasic login">' . translate( 'U moet ingelogd zijn om iets toe te voegen' ) . '</div>';
			
		}*/
		
		$this->js = "
			mod = new SocialAdd( " . json_encode( $this->vars ) . " );
			mod.showOrHide();
		";

		return $res;				
	}
	
	
	
	public function ajax_check_user() {
	
		$login = 0;
		if( $_SESSION[ 'uid' ] )
			$login = 1;
			
		return json_encode( array_merge( array( 'mod' => $this->render_mod( $this->vars->vars->id, $this->vars->vars->personal )), array( 'vars' => (array) $this->vars->vars ), array( 'login' => $login )));
	}
	
	
	public function render_mod( $id, $personal = true ) {
	
		if( !$personal ) {
		
			$Modcon = new MOD_CONSTRUCT();
			$Module = $Modcon->call( $id );
		
			unset( $Module->nameid );
		
			$mod = $Module->output();
			$css = file_get_contents( engine_get( 'url' ) . '/style.mod?mods=' . $this->vars->id );
			$jsc = file_get_contents( engine_get( 'url' ) . '/script.mod?mods=' . $this->vars->id );
		
			$res .= '<style type="text/css">' . $css . '</style>';
			$res .= '<script type="text/javascript">' . $jsc . '</script>';
			//$res .= '<span class="add">' . translate( $this->vars->txt ) . '</span>';
			$res .= $mod;
		
		} else if( $personal && !empty( $_SESSION[ 'uid' ] )) { 
		
			$Modcon = new MOD_CONSTRUCT();
			$Module = $Modcon->call( $id );
		
			unset( $Module->nameid );
		
			$mod = $Module->output();
			$css = file_get_contents( engine_get( 'url' ) . '/style.mod?mods=' . $id );
			$jsc = file_get_contents( engine_get( 'url' ) . '/script.mod?mods=' . $id );
		
			$res .= '<style type="text/css">' . $css . '</style>';
			$res .= '<script type="text/javascript">' . $jsc . '</script>';
			//$res .= '<span class="add">' . translate( $this->vars->txt ) . '</span>';
			$res .= $mod;
		
		} else {
			
			//$res .= '<span class="add">' . translate( $this->vars->txt ) . '</span>';
			$res .= translate( 'U moet ingelogd zijn om iets toe te voegen' );
			
		}
		
		return $res;
	
	}
	
}


